1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| package com.onlyloveyd.weather; import XXXX;
public class WeatherActivity extends Activity {
private static final String TAG = "MainActivity"; @Bind(R.id.imageview) ImageView imageview; @Bind(R.id.tv_cityname) TextView tvCityname; @Bind(R.id.tv_date) TextView tvDate; @Bind(R.id.iv_zhengfu) ImageView ivZhengfu; @Bind(R.id.iv_tenth) ImageView ivTenth; @Bind(R.id.iv_geth) ImageView ivGeth; @Bind(R.id.iv_du) ImageView ivDu; @Bind(R.id.iv_weather_fa) ImageView ivWeatherFa; @Bind(R.id.tv_weather) TextView tvWeather; @Bind(R.id.iv_weather_fb) ImageView ivWeatherFb; @Bind(R.id.tv_temp) TextView tvTemp; @Bind(R.id.tv_humidity) TextView tvHumidity; @Bind(R.id.tv_wind) TextView tvWind; @Bind(R.id.tv_refreshtime) TextView tvRefreshtime; @Bind(R.id.gv_future) GridView gvFuture; WeatherInfo.ResultBean.SkBean mSk; WeatherInfo.ResultBean.TodayBean mToday; FutureAdapter mFutureAdapter; ArrayList<WeatherInfo.ResultBean.FutureBean> mFutureList; private Context mContext; private String mCityName = null;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
mContext = this;
requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.weather); ButterKnife.bind(this); Intent intent = getIntent(); mCityName = intent.getStringExtra("cityname");
mFutureList = new ArrayList<WeatherInfo.ResultBean.FutureBean>(); mFutureAdapter = new FutureAdapter(this, mFutureList); gvFuture.setAdapter(mFutureAdapter); }
@Override protected void onResume() { super.onResume(); mFutureList.clear(); getWeatherInfoByCity(this, mCityName); tvCityname.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onBackPressed(); } }); }
private void getWeatherInfoByCity(Context context, String city) {
Log.e(TAG, "getWeatherInfoByCity"); Parameters param = new Parameters(); param.add("cityname", city); param.add("format", 2);
JuheData.executeWithAPI(context, 39, "http://v.juhe.cn/weather/index", JuheData.GET, param, new DataCallBack() { @Override public void onSuccess(int statusCode, String responseString) { Gson gson = new Gson(); try { JSONObject jsonObject = new JSONObject(responseString); JSONObject resultjson = jsonObject.getJSONObject("result"); JSONObject skjson = resultjson.getJSONObject("sk"); JSONObject todayjson = resultjson.getJSONObject("today"); JSONArray futurejson = resultjson.getJSONArray("future"); mSk = gson.fromJson(skjson.toString(), WeatherInfo.ResultBean.SkBean.class); mToday = gson.fromJson(todayjson.toString(), WeatherInfo.ResultBean.TodayBean.class); Log.e(TAG, futurejson.get(2).toString()); for (int i = 0; i < futurejson.length(); i++) { WeatherInfo.ResultBean.FutureBean futureBean = gson.fromJson(futurejson.get(i).toString(), WeatherInfo.ResultBean.FutureBean.class); Log.e(TAG, futureBean.toString()); mFutureList.add(futureBean); } mFutureAdapter.setmFutureList(mFutureList); gvFuture.setAdapter(mFutureAdapter); mFutureAdapter.notifyDataSetChanged();
tvCityname.setText(mCityName); tvDate.setText(mToday.getDate_y() + " " + mToday.getWeek()); tvWeather.setText(mToday.getWeather()); tvTemp.setText(mToday.getTemperature()); tvHumidity.setText(mSk.getHumidity()); tvWind.setText(mSk.getWind_direction() + " " + mSk.getWind_strength()); tvRefreshtime.setText(getString(R.string.refreshtimesuffix) + mSk.getTime());
String fa = mToday.getWeather_id().getFa(); String fb = mToday.getWeather_id().getFb(); if (fa.equals(fb)) { ivWeatherFa.setImageResource(getWeatherDrawableByWeatherId("w_", fa)); ivWeatherFb.setImageDrawable(null); } else { ivWeatherFa.setImageResource(getWeatherDrawableByWeatherId("w_", fa)); ivWeatherFb.setImageResource(getWeatherDrawableByWeatherId("w_", fb)); }
int tmp = Integer.valueOf(mSk.getTemp()); if (tmp < 0) { ivZhengfu.setImageResource(R.drawable.fuhao); int tenth = (0 - tmp) / 10; int geth = (0 - tmp) % 10; ivTenth.setImageResource(getWeatherDrawableByWeatherId("org4_widget_nw", String.valueOf(tenth))); ivGeth.setImageResource(getWeatherDrawableByWeatherId("org4_widget_nw", String.valueOf(geth))); } else { ivZhengfu.setImageDrawable(null); int tenth = tmp / 10; int geth = tmp % 10; ivTenth.setImageResource(getWeatherDrawableByWeatherId("org4_widget_nw", String.valueOf(tenth))); ivGeth.setImageResource(getWeatherDrawableByWeatherId("org4_widget_nw", String.valueOf(geth))); }
} catch (JSONException e) { e.printStackTrace(); }
}
@Override public void onFinish() { }
@Override public void onFailure(int statusCode, String responseString, Throwable throwable) { Log.e(TAG, responseString); } }); }
private int getWeatherDrawableByWeatherId(String suffix, String fa) { String res = suffix + fa; int resid = getResources().getIdentifier(res, "drawable", getPackageName()); return resid; }
@Override public boolean onTouchEvent(MotionEvent event) { final int action = event.getAction(); float startX = 0;
switch (action) { case MotionEvent.ACTION_DOWN: startX = event.getRawX(); break; case MotionEvent.ACTION_UP: float endX = event.getRawX(); if ((int) (endX - startX) > 10) { onBackPressed(); } break; default: break; }
return true; } }
|